﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace IOTestMain.Classes
{
    public class TypeReflection
    {
        public void TypeReflectionMain()
        {
            showMethods(typeof(DateTime));

            Console.WriteLine("=======================================");

            // TODO: Get all class name in same namespace
            Type[] typelist = Assembly.GetExecutingAssembly().GetTypes();// GetTypesInNamespace(Assembly.GetExecutingAssembly(), "MyNamespace");
            for (int i = 0; i < typelist.Length; i++)
            {
                Console.WriteLine(typelist[i].Name);
            }

            // TODO: get all interface
            // TODO: get all parent class
            // TODO: get all constructor
        }


        private Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
        {
            return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
        }

        private void showMethods(Type type)
        {
            foreach (var method in type.GetMethods())
            {
                var parameters = method.GetParameters();
                var parameterDescrpitions = string.Join
                    (", ",method.GetParameters().Select(x=>x.ParameterType+" "+x.Name).ToArray());

                Console.WriteLine("{0} {1} ({2})",
                    method.ReturnType,
                    method.Name,
                    parameterDescrpitions);
            }
        }
    }
}
